<?
/**
* Copyright (c) 2002-2007 BioWare Corp.
* http://www.bigdaddymonitor.org
*
* This file is part of the BigDaddy Monitoring
* program. It is free software distributed under
* the terms of the BSD License. It is ditributed
* in the hopes that it will be useful, but
* WITHOUT ANY WARRANTY; without event the implied
* warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE.
*
* The BSD License is available from:
* http://www.opensource.org/licenses/bsd-license.php
**/
?>

 <script language="javascript">

// -------------------------------------------------------------------
// swapOptions(select_object,option1,option2)
//  Swap positions of two options in a select list
// -------------------------------------------------------------------
function swapOptions(i,j) {
	//var o = obj.options;
	var i_selected = document.cat.elements['catlist[]'].options[i].selected;
	var j_selected = document.cat.elements['catlist[]'].options[j].selected;
	var temp = new Option(document.cat.elements['catlist[]'].options[i].text, document.cat.elements['catlist[]'].options[i].value, document.cat.elements['catlist[]'].options[i].defaultSelected, document.cat.elements['catlist[]'].options[i].selected);
	var temp2= new Option(document.cat.elements['catlist[]'].options[j].text, document.cat.elements['catlist[]'].options[j].value, document.cat.elements['catlist[]'].options[j].defaultSelected, document.cat.elements['catlist[]'].options[j].selected);
	document.cat.elements['catlist[]'].options[i] = temp2;
	document.cat.elements['catlist[]'].options[j] = temp;
	document.cat.elements['catlist[]'].options[i].selected = j_selected;
	document.cat.elements['catlist[]'].options[j].selected = i_selected;
	}


// -------------------------------------------------------------------
// moveOptionUp(select_object)
//  Move selected option in a select list up one
// -------------------------------------------------------------------
function moveOptionUp() {
	// If > 1 option selected, do nothing
	var selectedCount=0;
	for (i=0; i<document.cat.elements['catlist[]'].options.length; i++) {
		if (document.cat.elements['catlist[]'].options[i].selected) {
			selectedCount++;
			}
		}
	if (selectedCount > 1) {
		return;
		}
	// If this is the first item in the list, do nothing
	var i = document.cat.elements['catlist[]'].selectedIndex;
	if (i == 0) {
		return;
		}
	swapOptions(i,i-1);
	document.cat.elements['catlist[]'].options[i-1].selected = true;
	}

// -------------------------------------------------------------------
// moveOptionDown(select_object)
//  Move selected option in a select list down one
// -------------------------------------------------------------------
function moveOptionDown() {
	// If > 1 option selected, do nothing
	var selectedCount=0;
	for (i=0; i<document.cat.elements['catlist[]'].options.length; i++) {
		if (document.cat.elements['catlist[]'].options[i].selected) {
			selectedCount++;
			}
		}
	if (selectedCount > 1) {
		return;
		}
	// If this is the last item in the list, do nothing
	var i = document.cat.elements['catlist[]'].selectedIndex;
	if (i == (document.cat.elements['catlist[]'].options.length-1)) {
		return;
		}
	swapOptions(i,i+1);
	document.cat.elements['catlist[]'].options[i+1].selected = true;
	}

function callsubmit() {
	for (i=0; i<document.cat.elements['catlist[]'].options.length; i++) {
		document.cat.elements['catlist[]'].options[i].selected = true;
		
	}
	
	document.cat.submit();
}

</script>

<?

$sql="SELECT * FROM treeEntry WHERE ID='".$_REQUEST["ID"]."'";
$entryInfo=mysql_fetch_array(run_mysql_query($sql));

if ($entryInfo["ID"]=="" || $entryInfo["ID"]==0)
{
?>
	<p>You are ordering the root entries of the treeview</p>
<?
}
else
{
	$rootPath=rootEntryPathCreate($entryInfo["ID"]);
?>
	<p>You are ordering entries under<br /> <b>BioWare Home / <?=$rootPath;?></b></p>
<?
}
?>

<?
 
$sql="SELECT ID, name FROM treeEntry WHERE parentID=".$_REQUEST["ID"]." ORDER BY ord ASC";
$elements=run_mysql_query($sql);

if (@mysql_num_rows($elements)>0)
{

?>
	<table border="0" cellpadding="5" cellspacing="1" class="tablecolor1">
	<tr><td class="tablecolor2">
	<form method="post" name="cat" action="query_treeview.php">
	<select name="catlist[]" size="<?=mysql_num_rows($elements);?>" multiple>
	<?
	
	$i=1;
	while ($row=mysql_fetch_array($elements)) {
		echo "<option value=\"".$row["ID"]."\">".$i.". ".$row["name"]."</option>";
	$i++;
	}
	?>
	
	</select>
	<br /><br />
	<input type="button" value="Up" onClick="moveOptionUp()" />
	<input type="button" value="Down" onClick="moveOptionDown()" />
	
	<br /><br />
	<input type="hidden" name="action" value="orderChildren" />
	<input type="button" value="Order Children" onclick="callsubmit()" />
	
	</form>	
	</td></tr></table>

<?
}
else
{
?>

	<p><b>There are no elements to order.</b></p>
<?
}
?>
